﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Go
{
	class Helpers
	{
		public static bool is_point_an_eye(Board board, Point point, Player color)
		{
			if (board.get(point) != Player.none)
				return false;

			foreach (var neighbor in point.neighbors())
			{
				if (board.is_on_grid(neighbor))
				{
					var neighbor_color = board.get(neighbor);
					if (neighbor_color != color)
						return false;
				}
			}
			int friendly_corners = 0;

			int off_board_corners = 0;

			var corners = new Point[] 
			{
				new Point(point.row - 1, point.col - 1),
				new Point(point.row + 1, point.col + 1),
				new Point(point.row - 1, point.col + 1),
				new Point(point.row + 1, point.col - 1),
			};

			foreach (var corner in corners)
			{
				if (board.is_on_grid(corner))
				{
					var corner_color = board.get(corner);
					if (corner_color == color)
						friendly_corners += 1;
				}
				else
				{
					off_board_corners += 1;
				}
			}
			if (off_board_corners > 0)
				return off_board_corners + friendly_corners == 4;
			return friendly_corners >= 3;
		}
	}
}
